package rtti;

import framework.AbstractTest;
import utils.Log;

import java.util.Random;

/**
 * Created by shane on 2018/6/12.
 */

class Initable {
    static final int staticFinal = 47;
    static final int staticFinal2 = ClassInitialization.rand.nextInt(1000);

    static {
        System.out.println("Initialzing Initable");
    }
}

class Initable2 {
    static int staticNoFinal = 147;

    static {
        System.out.println("Initialzing Initable2");
    }
}


class Initable3 {
    static int staticNoFinal = 74;

    static {
        System.out.println("Initialzing Initable3");
    }
}


public class ClassInitialization extends AbstractTest {

    public static Random rand = new Random(47);

    @Override
    public void test(String[] args) {
        super.test(args);

        Class initable = Initable.class;

        Log.vn("After creating Initable ref");
        // Does not trigger initialization
        Log.vn(Initable.staticFinal);

        // Does trigger initialization
        Log.vn(Initable.staticFinal2);

        // Does trigger initialization
        Log.vn(Initable2.staticNoFinal);

        try {
            Class initable3 = Class.forName("rtti.Initable3");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Log.vn("After creating Initable3 ref");
        Log.vn(Initable2.staticNoFinal);
    }
}
